<cfparam default="none" name="url.api" />
<cfparam default="-1" name="url.StepID" />

<!---Need to call BASE functions--->
<cfset BASE = CreateObject("component", "cfc.base") />
<cfset baseQueries = CreateObject("component", "#session.rootDir#.cfc.queries") />
<cfset step3Queries = CreateObject("component", "#session.rootDir#.cfc.step3Queries") />
<cfinclude template="/code_lib/SCF_Employee.cfm">
<cfinclude template="/code_lib/SCF_HR.cfm">
<cfinclude template="/code_lib/SCF_REGEX.cfm">

<!---Scott's old code--->
<!--- Declare Constants --->
<cfset LOCALS = {} />
<cfset LOCALS.DOMAINCONTROLLER = "10.100.1.101" />
<cfset LOCALS.HOMEDIR = "G:" />
<cfset LOCALS.EMAILSUFFIX = "@scf.cc">
<!--- End Constants --->
<!--- Scott's old code --->


	<cfif url.api eq "AD_creation" >    
        <cfswitch expression="#CGI.REQUEST_METHOD#">
            <cfcase value="POST" >
            
            	<cfsetting enablecfoutputonly="true">
                
                <!---Query objects--->
				<cfset QUERIES = {} />
                <cfset QUERIES.GetEmpInfo = baseQueries.funcGetEmpInfo(URL.StepID) />

                <!---AD Creation Thread START--->

				<!---Scott's old code--->                    
                <cfset thread.Trace = "start" />
                <cfset thread.ADStarted = True />                     
                <cfset thread.ADResult ="Error Occured Creating AD Account"> <!---"<strong>Account Created Successfully</strong>"--->
                <cfset thread.Result = 0>  
                <cfset thread.NewUserID = ""> <!---"xTest1"--->
                <cfset thread.NewADGUID = "">  
                <cfset thread.TempResult =""> 
                <cfset thread.Warning = False>  
                <!---Scott's old code--->         	
                
                <cfif QUERIES.GetEmpInfo.NonSCFNetworkAccount EQ 1> 
                
                    <cfset thread.ADResult = "Error Occured Creating AD Account">   <!--- Preset only --->
                
                    <cftry>                        
                    
                        <cfset thread.Trace = "1" >
                         
                        <cfset QUERIES.GetNextMailServer = baseQueries.funcGetNextMailServer() />
                        <cfset QUERIES.GetContainer = baseQueries.funcGetContainer(QUERIES.GetEmpInfo.ContainerID) />
                        <cfset QUERIES.GetDept = baseQueries.funcGetDepts(QUERIES.GetEmpInfo.DeptID) />
                        <cfset QUERIES.GetNonSCFType = baseQueries.funcGetNonSCFTypes(0, QUERIES.GetEmpInfo.NonSCFTypeID) />
						
                        <cfset thread.Trace = "1.2">
                        
                        <cfif QUERIES.GetEmpInfo.MirrorAccountID NEQ 0 and QUERIES.GetEmpInfo.MirrorAccountID NEQ ''>
                            <cfset LOCALS.Mirror = SCF_Employee_GetEmployeeInfo_Basic(QUERIES.GetEmpInfo.MirrorAccountID)>
                            <cfset LOCALS.Mirror = LOCALS.Mirror.Login>
                        <cfelse>
                            <cfset LOCALS.Mirror = ''> 
                        </cfif>
        				
                        <cfset thread.Trace = "1.3">
                        
                        <cfset LOCALS.Type = "SCF " & QUERIES.GetNonSCFType.Type />
                        
                        <cfif trim(QUERIES.GetEmpInfo.BadgePos) NEQ ''>
                            <cfset LOCALS.BadgePos = QUERIES.GetEmpInfo.BadgePos>
                        <cfelse>
                            <cfset LOCALS.BadgePos = LOCALS.Type>
                        </cfif>
                        
                        <cfset thread.Trace = "1.4">
                        
                        <cfset thread.ADResult = "Error Occured Creating AD Account - .NET Creation" />
                    
                        <cfset thread.Trace = "2">
                        
                        <cfobject type="dotnet" name="ADCreation" class="ADTools.Creation" assembly ="C:\Program Files\Southcentral Foundation\ADTools\ADTools.dll">
                        
                        <cfset thread.Trace = "3">
                                                
                        <cfset thread.Result = ADCreation.CreateUser(
                            #LOCALS.DOMAINCONTROLLER#, 
                            #replace(QUERIES.GetEmpInfo.First," ","","all")#, 
                            #QUERIES.GetEmpInfo.Middle#, 
                            #replace(QUERIES.GetEmpInfo.Last," ","","all")#,
                            #replace(QUERIES.GetEmpInfo.PreferredName," ","","all")#, 
                            #SCF_Employee_GetEmployeeInfo_Basic(QUERIES.GetEmpInfo.SupervisorID).Email#, 
                            #QUERIES.GetEmpInfo.CompanyName#, 
                            #QUERIES.GetDept.OrganizationDescription#,
                            #LOCALS.BadgePos#,
                            #LOCALS.Type#,
                            #QUERIES.GetContainer.HomeDir#,
                            #LOCALS.HOMEDIR#,
                            #QUERIES.GetNextMailServer.ServerName#,
                            "OU=" & #QUERIES.GetContainer.Container# & ";",
                            #QUERIES.GetEmpInfo.DeskPhone#,
                            #LOCALS.Mirror#,
                            #dateformat(QUERIES.GetEmpInfo.ExpDate,'mm/dd/yyyy')#, 
                            10000) />   
                        
                        <cfset thread.Trace = "4">
                        
                        <cfif thread.Result NEQ 0>
                        
                            <cfset thread.ADResult = replace(replace(replace(replace(ADCreation.GetErrorText(),chr(13),'<BR/>','all'), chr(10), '','all'),'<BR/>',''),'<BR/><BR/>','')  />   
                            <cfif thread.Result EQ -2>                            
                                <cfset thread.Success = True>
                                <cfset thread.Warning = True>                                
                            <cfelse>
                                <cfset thread.Success = False> 
                            </cfif>
                            
                        <cfelse>                            
                            <cfset thread.Success = True>  
                            <cfset thread.ADResult =  ''>                             
                        </cfif>
                        
                        <cfset thread.Trace = "5">
                        
                        <cfif thread.Success>
                        
                            <cfset thread.Result = 0>
                            
                            <cfset thread.NewUserID = ADCreation.GetNewLogin() />   
                            <cfset thread.NewADGUID = ADCreation.GetGUID(thread.NewUserID, "", "")/>
                            
                            <cfset strTrace = "5.1">
                            
                            <cfif QUERIES.GetNextMailServer.ServerID GTE 3>
                                <cfset LOCALS.Next = 1>
                            <cfelse>		
                                <cfset LOCALS.Next = QUERIES.GetNextMailServer.ServerID + 1>
                            </cfif>   
                            
                            <cfset thread.Trace = "5.2">  
                            
                            <cfset QUERIES.UpdateNextServer = step3Queries.funcUpdateNextServer(LOCALS.Next) />
                            
                            <cfset thread.Trace = "5.3">
                            
                            <cfset thread.TempResult ="<strong>Account Created Successfully</strong><BR>">
                            
                            <cfif thread.Warning EQ True>                                 
                                 <cfset thread.TempResult = thread.TempResult & "<u>WARNING</u>:&nbsp;&nbsp;">                                     
                            </cfif>
                            
                            <cfset thread.ADResult = thread.TempResult & thread.ADResult> 
                            
                            <cfset Error = False>  
                            
                            <cfset thread.Trace = "5.4." & thread.Result & " " & thread.ADResult>
                            
                            <cfif thread.Warning EQ True>
                            
                                <!--- Send notice to the helpdesk that some action is required--->                            
                                <cfmail to=#Session.SERVICEDESK_EMAIL# From="EPAR_Warning@scf.cc" subject="EPAR Warning:  Creating Active Directory Account" type="html">
                                    Error generating the Active Directory record<BR /><BR />
                                    Result: #thread.Result# #thread.ADResult# 
                                </cfmail>
                                
                            </cfif>
                            
                            <!---return JSON object with status code and NewUserID--->
							<cfset retObj = {
                                NewUserID = thread.NewUserID,
								NewADGUID = thread.NewADGUID,
                                Status = 'Created',
                                Message = thread.ADResult
                            } />
                            <cfoutput>#SerializeJSON(retObj, true)#</cfoutput>
                            
                        <cfelse>
                        
                            <cfset thread.NewUserID = ""> 
                            <cfset thread.ADResult = thread.ADResult & " " & thread.Trace> 
                            <cfset Error = True>
                            
                            <cfmail to=#Session.SERVICEDESK_EMAIL# From="EPAR_Error@scf.cc" subject="EPAR Error:  Creating Active Directory Account" type="html">
                                Error generating the Active Directory record<BR /><BR />
                                Trace: #thread.Trace#<BR />
                                Result: #thread.Result# #thread.ADResult#
                            </cfmail>
                            
                            <!---return JSON object with status code and NewUserID--->
							<cfset retObj = {
                                Status = 'Fail',
                                Message = 'Error generating the Active Directory record<BR /><BR />
                                Trace: #thread.Trace#<BR />
                                Result: #thread.Result# #thread.ADResult#' 
                            } />
                            <cfoutput>#SerializeJSON(retObj, true)#</cfoutput>
                            
                        </cfif>
                    
                        <cfset thread.Trace = "6">
                        
                        <cfcatch type="any">
                        
                            <cfset thread.ADResult = CFCATCH.Detail>
                            <cfset thread.NewUserID = "">  
                            <cfset thread.Result = -1>
                            <cfset Error = True>
                            
                            <cfset QUERIES.InsertErrorLog = baseQueries.funcInsertErrorLog(
								#URL.StepID#, '#Session.intUserRosettaID#', 'Error generating the Active Directory record: 
								#CFCATCH.Detail#  Trace: #thread.Trace#  Result:#thread.Result# #thread.ADResult#') 
							/>
                            
                            <cfmail to=#Session.SERVICEDESK_EMAIL# From="EPAR_Error@scf.cc" subject="EPAR Error:  Creating Active Directory Account" type="html">
                                Error generating the Active Directory record:  #CFCATCH.Detail#<BR /><BR />
                                Trace: #thread.Trace#<BR />                           
                            </cfmail>
                            
                            <!---return JSON object with status code and NewUserID--->
							<cfset retObj = {
                                Status = 'Fail',
								Message = 'Error generating the Active Directory record: #CFCATCH.Detail#  
									Trace: #thread.Trace#  Result:#thread.Result# #thread.ADResult#'
                            } />
                            <cfoutput>#SerializeJSON(retObj, true)#</cfoutput>                            
                        </cfcatch>                        
                    </cftry>                   
                        
                </cfif>
                <!---Scott's old code---> 
                <!---AD Creation Thread FINISH--->
            </cfcase>    
        </cfswitch>
    </cfif>
    
    
        
    
    <cfif url.api eq "Rosetta_creation" >    
        <cfswitch expression="#CGI.REQUEST_METHOD#"> 
            <cfcase value="POST" >
            	
                <cfsetting enablecfoutputonly="true"> 
                            
                <cftry> 
                	
                    <cfset thread.Trace = "1">
                    
                    <cfset thread.Result = "" />
                    <cfset thread.RosettaID = 0 />
                    <cfset thread.EPAR_ID = 0 />
					<cfset thread.RosettaStarted = True />
                    
                    <cfset thread.Trace = "1.0.1">
                    
                    <cfset QUERIES = {} />
            		<cfset QUERIES.GetEmpInfo = baseQueries.funcGetEmpInfo(URL.StepID) />
                    <cfset QUERIES.GetContainer = baseQueries.funcGetContainer(QUERIES.GetEmpInfo.ContainerID) />
                    <cfset QUERIES.GetDept = baseQueries.funcGetDepts(QUERIES.GetEmpInfo.DeptID) />
                    <cfset QUERIES.GetNonSCFType = baseQueries.funcGetNonSCFTypes(0, QUERIES.GetEmpInfo.NonSCFTypeID) />       
                    <cfset QUERIES.Supervisor = SCF_Employee_GetEmployeeInfo_Basic(QUERIES.GetEmpInfo.SupervisorID)>
                    
                    <cfset thread.Trace = "1.1">
                    
					<cfset thread.UserID = FORM.NewUserID />
                    <cfset thread.AD_GUID = FORM.NewADGUID />
                    <cfset thread.HRProdID = 0 />
                    <cfset thread.EPAR_ID = QUERIES.GetEmpInfo.ID />
                    
                    <cfset thread.Trace = "2">
                    
                    <!--- BAD BRUCE BAD!!!!!! --->
                    <cfif len(trim(QUERIES.GetEmpInfo.DeskPhone)) EQ 4>
                        <cfset LOCALS.WPhone = '907729' & QUERIES.GetEmpInfo.DeskPhone />
                    <cfelse>
                        <cfset LOCALS.WPhone = BASE.CleanPhone(QUERIES.GetEmpInfo.DeskPhone) />
                    </cfif> 
                                       
                    <cfset EmployeeOBJ = {
                        First = QUERIES.GetEmpInfo.First,
                        Last = QUERIES.GetEmpInfo.Last,
                        Pref = QUERIES.GetEmpInfo.PreferredName,
                        Middle = QUERIES.GetEmpInfo.Middle,
                        SSN = QUERIES.GetEmpInfo.SSN,
                        Email = thread.UserID & LOCALS.EMAILSUFFIX,
                        WPhone = LOCALS.WPhone,
                        Company = QUERIES.GetEmpInfo.CompanyName,
                        Dept = QUERIES.GetDept.OrganizationDescription,
                        Supervisor = QUERIES.Supervisor.FirstName & ' ' & QUERIES.Supervisor.LastName & " (" & QUERIES.Supervisor.Login & ")",
                        TypeID = QUERIES.GetEmpInfo.NonSCFTypeID
                    } />
                    
                    <cfset thread.Trace = "2.01">

                    <cfset QUERIES.CreateNonSCFMasterRecord = baseQueries.funcCreateNonSCFMasterRecord( EmployeeOBJ ) />
                    
                    <cfset thread.Trace = "2.02">
                    
                    <cfset thread.NonSCFMasterID = QUERIES.CreateNonSCFMasterRecord.NewID />
                    
                    <cfset QUERIES.CreateRosettaRecord = baseQueries.funcCreateRosettaRecordNonSCF( thread ) />
                    
                    <cfset thread.Trace = "3">
                    
                    <cfset thread.RosettaID = QUERIES.CreateRosettaRecord.NewRosettaID />
                    
                    <cfset thread.Trace = "3.1">
                    
                    <cfif thread.UserID NEQ '' and QUERIES.GetEmpInfo.NonSCFNetworkAccount EQ 0>
                    	
                        <cfset thread.Trace = "3.2">
                        
                        <cfset thread.Result = "The Rosetta Stone Record was created, but there was a problem creating the employee\'s HR record.
                            The Network Account has been created. You\'ll need to manually add the employee to Kronos HR.
                            After the employee has been added, please notify the Service Desk so that the mapping can be completed in Rosetta Stone.">
                            
                    <cfelseif thread.UserID NEQ '' and QUERIES.GetEmpInfo.NonSCFNetworkAccount GT 0> 
                    	
                        <cfset thread.Trace = "3.3">
                        
                        <cfset thread.Result = "<strong>Success</strong>" />
                        
                        <cfset EmployeeNameDisplay = QUERIES.GetEmpInfo.First />
                         
						<cfif trim(QUERIES.GetEmpInfo.Middle) NEQ ''>
                            <cfset EmployeeNameDisplay &= ' #QUERIES.GetEmpInfo.Middle#' />
                        </cfif>
                        
                        <cfif trim(QUERIES.GetEmpInfo.PreferredName) NEQ ''>
                            <cfset EmployeeNameDisplay &= ' (#QUERIES.GetEmpInfo.PreferredName#)' />
                        </cfif>                        
                        
                        <cfset thread.Trace = "4">
                    
	                    <!---return JSON object with status code and NewUserID--->
	                    <cfset retObj = {
	                        Status = 'Created',
	                        Message = thread.Result,
	                        NewUserID = thread.UserID,
	                        NewADGUID = thread.AD_GUID,
	                        NewRosettaID = thread.RosettaID
	                    } />
	                    <cfoutput>#SerializeJSON(retObj, true)#</cfoutput>
                
                        <cfmail bcc="#Session.ITS_EMAIL#" to="#SCF_Employee_GetEmployeeInfo_Basic(QUERIES.GetEmpInfo.SupervisorID).Email#" 
                        	from="""HR Dept - EPAR System"" <noreply@scf.cc>"  type="html" subject="Employee Hire Update">
                            <p>The employee, <strong>#EmployeeNameDisplay#</strong>, is now in the SCF system.  
                                Here is some information regarding their status and user account.<BR />
                            </p>
                            
                            <table>
                                    <tr>
                                        <td width=5>&nbsp;</td>
                                        <td>Network User Account:</td>
                                        <td width=25>&nbsp;</td>
                                        <td>
                						<cfset thread.Trace = "4" & thread.AD_GUID >  
										<cfif QUERIES.GetEmpInfo.NonSCFNetworkAccount EQ 1>
                                        	#thread.UserID#   
                                        <cfelse>
                                        	None
                                        </cfif>                      	
                                        </td>
                                    </tr>
                                    
                					<cfset thread.Trace = "5" & thread.AD_GUID>  
                                    <cfif QUERIES.GetEmpInfo.NonSCFNetworkAccount EQ 1>
                                        <tr>
                                            <td>&nbsp;</td>
                                            <td>Password (required to change @ first login):</td>
                                            <td>&nbsp;</td>
                                            <td>Password10! (case sensitive)</td>
                                        </tr>
                                        <tr>
                                            <td>&nbsp;</td>
                                            <td>Home Directory:</td>
                                            <td>&nbsp;</td>
                                            <td>#QUERIES.GetContainer.HomeDir#\#thread.UserID#</td>
                                        </tr>
                                    </cfif>
                					<cfset thread.Trace = "6" & thread.AD_GUID>  
                                    <tr>
                                        <td>&nbsp;</td>
                                        <td>Start Date:</td>
                                        <td>&nbsp;</td>
                                        <td>#dateformat(QUERIES.GetEmpInfo.ActStartDate,'mm/dd/yyyy')#</td>
                                    </tr>
                					<cfset thread.Trace = "7" & thread.AD_GUID>  
                                    <tr>
                                        <td>&nbsp;</td>
                                        <td>Expiration Date:</td>
                                        <td>&nbsp;</td>
                                        <td>#dateformat(QUERIES.GetEmpInfo.ExpDate,'mm/dd/yyyy')#</td>
                                    </tr>
                					<cfset thread.Trace = "8" & thread.AD_GUID>  
									<cfif QUERIES.GetEmpInfo.DeskPhone NEQ "">
                                        <tr>
                                            <td>&nbsp;</td>
                                            <td>Desk Phone:<BR />(as it will appear in the Address Book)</td>
                                            <td>&nbsp;</td>
                                            <td>#QUERIES.GetEmpInfo.DeskPhone#</td>
                                        </tr>
									</cfif>
                                </table>                    	
                        </cfmail> 
                    </cfif>                                                         
                    
                    <cfcatch type="any">
                    
                        <cfset thread.Result &= '<BR> #CFCATCH.Detail#'>
                        <cfset thread.Result = -1>
                        <cfset variables.Error = True>
                        
                        <cfset QUERIES.InsertErrorLog = baseQueries.funcInsertErrorLog(
							URL.StepID, Session.intUserRosettaID, 'Error creating the Rosetta Stone record: #CFCATCH.Detail#' 
						) />
                        
                        <cfmail to=#Session.SERVICEDESK_EMAIL# From="EPAR_Error@scf.cc" subject="EPAR Error:  Creating Rosetta Stone Record" type="html">
                            Error creating the Rosetta Stone record: #CFCATCH.Detail#<BR /><BR />
                            Step ID: #URL.StepID#<BR />
                            Supervisor Email: #SCF_Employee_GetEmployeeInfo_Basic(QUERIES.GetEmpInfo.SupervisorID).Login#@scf.cc<BR />
                            Action by User (Rosetta ID): #Session.intUserRosettaID#
                        </cfmail>
                        
                        <!---return JSON object with status code and NewUserID--->
						<cfset retObj = {
                            Status = 'Fail',
							Trace = thread.Trace,
                            Message = 'Error creating the Rosetta Stone record: #CFCATCH.Detail#<BR /><BR />
								Trace: #thread.Trace#<br />
								Step ID: #URL.StepID#<BR />
								Supervisor Email: #SCF_Employee_GetEmployeeInfo_Basic(QUERIES.GetEmpInfo.SupervisorID).Login#@scf.cc<BR />
								Action by User (Rosetta ID): #Session.intUserRosettaID#'
                        } />
                        <cfoutput>#SerializeJSON(retObj, true)#</cfoutput>
                
                    </cfcatch>                
                </cftry>                
                
            </cfcase>            
        </cfswitch>      
    </cfif>
    
    
    
    
    <cfif url.api eq "Creation_Complete" >    
        <cfswitch expression="#CGI.REQUEST_METHOD#">
            <cfcase value="POST" >
            	
                <cftry>
                
                	<cfsetting enablecfoutputonly="true">                           
                
                	<cfset QUERIES = {} />
            		<cfset QUERIES.GetEmpInfo = baseQueries.funcGetEmpInfo(URL.StepID) />                    
                
                    <cfset thread.FinalStarted = True>
                    
                    <cfset QUERIES.Step3UpdateAction = baseQueries.funcUpdateAction(URL.StepID, Session.intUserRosettaID, 'Auto Generated') />
                	<cfset QUERIES.Step3InsertAction = baseQueries.funcInsertAction(QUERIES.GetEmpInfo.ID, 5) />
                    
                    <!---return JSON object with status code and NewUserID--->
					<cfset retObj = {
                        Status = 'Created',
                        Message = '<strong>Success</strong>'
                    } />
                    <cfoutput>#SerializeJSON(retObj, true)#</cfoutput>
                    
                    <cfcatch type="any">
                        
                        <cfset QUERIES.InsertErrorLog = baseQueries.funcInsertErrorLog(
							URL.StepID, Session.intUserRosettaID, 'Error finalizing the step.' 
						) />
                        
                        <cfmail to=#Session.SERVICEDESK_EMAIL# From="EPAR_Error@scf.cc" subject="EPAR Error:  Advancing the Step" type="html">
                            Error advancing the step: #CFCATCH.Detail#<BR /><BR />
                            Step ID: #URL.StepID# ... Step 4 to 5<BR />
                            Action by User (Rosetta ID): #Session.intUserRosettaID#                            
                        </cfmail>
                        
                        <!---return JSON object with status code and NewUserID--->
						<cfset retObj = {
                            Status = 'Fail',
                            Message = 'Error advancing the step: #CFCATCH.Detail#<BR /><BR />
                            Step ID: #URL.StepID# ... Step 4 to 5<BR />
                            Action by User (Rosetta ID): #Session.intUserRosettaID# '
                        } />
                        <cfoutput>#SerializeJSON(retObj, true)#</cfoutput>
                    </cfcatch>
        
                </cftry>
                
            </cfcase>            
        </cfswitch>      
    </cfif>
